package com.broheim.websocket.core.listener;

import com.broheim.websocket.core.endpoint.context.ChannelContext;
import com.broheim.websocket.core.endpoint.context.ChannelContextManager;
import com.broheim.websocket.core.event.accept.Event;
import com.broheim.websocket.core.event.accept.OnCloseEvent;
import com.broheim.websocket.core.event.accept.OnConnectionEvent;

public class ChannelContextManagerListener implements Listener{
    @Override
    public void onEvent(Event event, ListenerChain chain) throws Exception {
        if (event instanceof OnConnectionEvent) {
            ChannelContext channelContext = ((OnConnectionEvent) event).getChannelContext();
            ChannelContextManager.add(channelContext);
        }

        if (event instanceof OnCloseEvent) {
            ChannelContext channelContext = ((OnCloseEvent) event).getChannelContext();
            ChannelContextManager.remove(channelContext);
        }
        chain.doChain(event);
    }
}
